<?php
/**
 * 安装程序
 * $Id: install.php 238 2011-01-13 08:53:47Z bwanglei@gmail.com $
 */

require_once './config.php';
require_once AW_ROOT.'/lib/class.mysql.php';
require_once AW_ROOT.'/lib/class.phpass.php';

$DB = MySQL::getInstance();?>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
	<title>安装程序</title>
	<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
</head><body><p style="text-align:center; width:550px; margin:50px auto 0px;"><form action="" method="post">
	<table><tr><td>网站名称：</td><td><input type="text" name="site_name" /></td></tr>
	<tr><td>用户名：</td><td><input type="text" name="username" /></td></tr>
	<tr><td>密码：</td><td><input type="password" name="password" /></td></tr>
	<tr><td></td><td align="right"><input type="submit" value="提交" /></td></tr></table>
</form></p></body></html>
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
//从表单获取用户名和密码
$username = $_POST['username'];
$password = $_POST['passowrd'];
//hash密码
$aw_hash = new PasswordHash(8,true);
$password = $aw_hash->HashPassword($password);
$site_url = 'http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['SCRIPT_NAME']);	//本站网址

$DB->query('SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"');
$DB->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."articles` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(256) NOT NULL,
  `content` text NOT NULL,
  `time_date` int(10) unsigned NOT NULL,
  `type` varchar(20) NOT NULL,
  `views` bigint(20) unsigned NOT NULL DEFAULT '0',
  `allow_remark` enum('n','y') NOT NULL DEFAULT 'y',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=39");

$DB->query("INSERT INTO `".DB_PREFIX."articles` (`id`, `title`, `content`, `time_date`, `type`, `views`, `allow_remark`) VALUES
(37, '关于', 'asdfasdf', 1290132314, 'page', 5, 'y'),
(38, 'asdf', 'asdfasdfasdf', 1290137301, 'article', 4, 'y'),
(36, '关于作者', '这是一个测试\r\n\r\nasdfasdf\r\n<br />\r\nasdfasdf<strong>asdfdsf</strong>', 1290132200, 'page', 5, 'y'),
(32, 'asdfasdfasd', 'asdfasdf', 1289541485, 'page', 37, 'y')");


$DB->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."comments` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `nickname` varchar(256) NOT NULL,
  `email` varchar(256) DEFAULT NULL,
  `url` varchar(256) DEFAULT NULL,
  `content` text NOT NULL,
  `aid` bigint(20) unsigned NOT NULL,
  `time_date` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=23");

$DB->query("INSERT INTO `".DB_PREFIX."comments` (`id`, `nickname`, `email`, `url`, `content`, `aid`, `time_date`) VALUES
(20, 'asdf', '', '', 'asdfasdf', 32, 1290132414),
(21, 'asdf', '', '', 'asdf', 32, 1290134766),
(22, 'asdf', '', '', 'asdfasdfasdf', 32, 1290135045)");

$DB->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."files` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `url` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=27");

$DB->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."links` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(256) NOT NULL,
  `url` varchar(256) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=22");

$DB->query("INSERT INTO `".DB_PREFIX."links` (`id`, `name`, `url`) VALUES
(1, 'Google', 'http://google.com/'),
(3, 'asdf', 'asdfasdf'),
(7, 'asfd', 'asdf'),
(8, 'adf', 'fasdf'),
(9, 'asdf', 'asdf'),
(10, 'asdf', ' asdf'),
(11, 'asdfasf', 'afsdf'),
(12, 'asdfsdf', 'sasfdsaf'),
(13, 'asdfsaf', 'asdfasdf'),
(14, 'adfasdf', 'asfd'),
(16, 'b', 'b'),
(17, 'c', 'c'),
(18, 'd', 'd'),
(19, 'e', 'e'),
(20, 'f', 'f'),
(21, 'g', 'g')");

$DB->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."settings` (
  `name` varchar(255) NOT NULL,
  `value` text NOT NULL,
  `type_one` varchar(8) NOT NULL DEFAULT 'y',
  `miaoshu` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8");

$pre_sql = "INSERT INTO `".DB_PREFIX."settings` (`name`, `value`, `type_one`, `miaoshu`) VALUES";
$sql = <<<EOT
{$pre_sql}
('timezone', '8', 'y', '设置时区'),
('index_comnum', '5', 'y', '首页侧栏显示评论个数'),
('comment_subnum', '2000', 'y', NULL),
('index_newlognum', '7', 'y', '每页显示日至数量'),
('blogUrl', '{$site_url}', 'y', '本站网址'),
('blogname', 'wanglei''s website', 'y', '网站名称'),
('bloginfo', '好好活做有意义的事', 'y', '网站副标题'),
('widget_title', 'a:9:{s:7:"blogger";a:1:{s:5:"title";s:12:"博客作者";}s:3:"tag";a:1:{s:5:"title";s:6:"标签";}s:7:"newcomm";a:1:{s:5:"title";s:12:"最新评论";}s:6:"search";a:1:{s:5:"title";s:6:"搜索";}s:7:"archive";a:1:{s:5:"title";s:6:"存档";}s:11:"custom_text";a:1:{s:5:"title";s:15:"自定义文本";}s:4:"link";a:1:{s:5:"title";s:12:"友情链接";}s:8:"bloginfo";a:1:{s:5:"title";s:12:"博客信息";}s:5:"admin";a:1:{s:5:"title";s:12:"博客管理";}}', 'n', NULL),
('widgets', 'a:9:{s:7:"blogger";a:1:{s:5:"title";s:12:"博客作者";}s:3:"tag";a:1:{s:5:"title";s:6:"标签";}s:7:"newcomm";a:1:{s:5:"title";s:12:"最新评论";}s:6:"search";a:1:{s:5:"title";s:6:"搜索";}s:7:"archive";a:1:{s:5:"title";s:6:"存档";}s:11:"custom_text";a:1:{s:5:"title";s:15:"自定义文本";}s:4:"link";a:1:{s:5:"title";s:12:"友情链接";}s:8:"bloginfo";a:1:{s:5:"title";s:12:"博客信息";}s:5:"admin";a:1:{s:5:"title";s:12:"博客管理";}}', 'n', NULL),
('viewcount_day', '1', 'y', NULL),
('viewcount_yesterday', '14', 'y', NULL),
('viewcount_all', '1957', 'y', NULL),
('viewcount_day_date', '1291939200', 'y', NULL),
('file_size', '2048', 'y', NULL)
EOT;
$DB->query($sql);
$DB->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."tags` (
  `aid` bigint(20) unsigned NOT NULL,
  `tag` varchar(255) NOT NULL,
  PRIMARY KEY (`aid`,`tag`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8");

$DB->query("INSERT INTO `".DB_PREFIX."tags` (`aid`, `tag`) VALUES
(6, 'linuxa'),
(6, 'zz-编程'),
(7, 'php'),
(9, 'linuxa'),
(9, 'php'),
(10, 'zz-编程'),
(17, 'php'),
(17, 'zz-编程'),
(18, 'php'),
(22, 'linuxa'),
(22, 'php'),
(22, 'zz-编程'),
(32, 'linuxa'),
(38, 'asdf')");

$DB->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."tags_atype` (
`aid` bigint(20) unsigned
,`tag` varchar(255)
,`type` varchar(20)
)");

$DB->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."users` (
  `name` varchar(256) NOT NULL,
  `password` varchar(256) NOT NULL,
  `nickname` varchar(256) NOT NULL,
  `email` varchar(256) NOT NULL,
  PRIMARY KEY (`name`),
  UNIQUE KEY `username` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8");

$DB->query("INSERT INTO `".DB_PREFIX."users` (`name`, `password`, `nickname`, `email`) VALUES
('".$username."', '".$password."', '".$username."', '')");

$DB->query("DROP TABLE IF EXISTS `".DB_PREFIX."tags_atype`");

$DB->query("CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `".DB_PREFIX."tags_atype` AS select `".DB_PREFIX."tags`.`aid` AS `aid`,`".DB_PREFIX."tags`.`tag` AS `tag`,`".DB_PREFIX."articles`.`type` AS `type` from (`".DB_PREFIX."articles` join `".DB_PREFIX."tags`) where ((`".DB_PREFIX."tags`.`aid` = `".DB_PREFIX."articles`.`id`) and (`".DB_PREFIX."articles`.`type` = 'article'))");

}
